<?php

/**
 *
 *
 * @version $Id: class_tpl.inc.php,v 1.13 2005/01/18 17:41:25 phppc Exp $
 * @copyright 2004
 **/

// Templateklasse -> PHP News Creator

/**
 * tpl
 *
 * @package
 * @author Armin
 * @copyright Copyright (c) 2004
 * @version $Id: class_tpl.inc.php,v 1.13 2005/01/18 17:41:25 phppc Exp $
 * @access public
 **/
class tpl extends session{
	var $tpl_name = "";
	var $actual_tpl_path = ""; // absolute path to the template
	var $actual_tpl_dir = "";
	var $actual_tpl = ""; // the actual template
	var $tpl_speicher = array();
	var $anzahl_tpls = 0;
	function tpl_load($tpl_file, $dir = "", $endung = ".htm") {
		$complete_path = $this->config["a_pfad"] . $this->config["sub_dir_tpl"] . $this->nc_style["tpl_dir"] . $dir . $tpl_file . $endung;
		$this->tpl_name = $tpl_file;
	    $this->actual_tpl_path = $complete_path;
	    $this->actual_tpl_dir = $dir;
		if(!isset($this->tpl_speicher["$complete_path"])) {
			$file = @str_replace ("\"","\\\"",implode("",file($complete_path)));
			$this->tpl_speicher["$complete_path"] = $file;
			$this->anzahl_tpls++;
		}else $file = $this->tpl_speicher["$complete_path"];
		if (!$file) $this->tpl_error("",$tpl_file,$dir);
	    $this->actual_tpl = $file;
	}
	/**
	 * tpl::tpl_bearb_nor()
	 *
	 * @return
	 **/
	function tpl_bearb_nor() {
	    $this->actual_tpl = preg_replace ("!\[LANG=(.*)\]!esiU","\$this->tpl_bearb_lang(stripslashes('\\1'))",$this->actual_tpl);
		$this->actual_tpl = preg_replace ("!\[INC=(.*)\]!esiU","\$this->tpl_bearb_inc(stripslashes('\\1'))",$this->actual_tpl);
		$this->actual_tpl = preg_replace ("!\[VAR=(.*)\]!esiU","\$this->tpl_bearb_var(stripslashes('\\1'))",$this->actual_tpl);
	    $this->actual_tpl = preg_replace ("!\{IF\((.*)\)\}(.*)\{\/IF\}!esiU","\$this->tpl_bearb_adv(stripslashes('\\1'),stripslashes('\\2'))",$this->actual_tpl);
	}
	/**
	 *
	 * @access public
	 * @return void
	 **/
	/**
	 * tpl::tpl_replace_vars()
	 *
	 * @param $replace_array
	 * @return
	 **/
	function tpl_replace_vars($replace_array){
		foreach ($replace_array as $search => $replace) {
			$this->actual_tpl = str_replace('%'.$search.'%',$replace,$this->actual_tpl);
		}
	}

	/**
	 * tpl::tpl_bearb_adv()
	 *
	 * @param $bedingung
	 * @param $dann_sonst
	 * @return
	 **/
	function tpl_bearb_adv($bedingung,$dann_sonst) {
		$bedingung = stripslashes($bedingung);
		$teilung = explode("{else}",$dann_sonst);
		if(!isset($teilung[1]))$teilung[1] = "";
		$dann = $teilung[0];
		$sonst = $teilung[1];
		$bed_wahr = false;
		eval ("if ($bedingung) \$bed_wahr = true;");
		if ($bed_wahr == true) return $dann;
		else return $sonst;
	}
	/**
	 *
	 * @access public
	 * @return void
	 **/
	/**
	 * tpl::tpl_bearb_inc()
	 *
	 * @param $module_intern
	 * @return
	 **/
	function tpl_bearb_inc($module_intern){
		if(!isset($this->inc_value["$module_intern"])) $this->inc_value["$module_intern"] = "";
		return $this->inc_value["$module_intern"];
	}

	/**
	 * tpl::tpl_bearb_lang()
	 *
	 * @param $lang_var
	 * @return
	 **/
	function tpl_bearb_lang($lang_var) {
		if(!isset($this->language[$lang_var])) $this->language[$lang_var] = "";
		return $this->language[$lang_var];
	}
	/**
	 * tpl::tpl_bearb_var()
	 *
	 * @param $var
	 * @return
	 **/
	function tpl_bearb_var($var) {
		return $this->$var;
	}
	/**
	 * tpl::tpl_output()
	 *
	 * @return
	 **/
	function tpl_output() { // parses the template
	    $this->tpl_bearb_nor();
		$return = stripslashes($this->actual_tpl);
		$this->actual_tpl = "";
		return $return;
	}
	/**
	 * tpl::tpl_error()
	 *
	 * @param string $error
	 * @param string $tpl_name
	 * @param string $dir
	 * @return
	 **/
	function tpl_error($error = "") { //outputs an error message
		echo('Template&nbsp;<b>'.$this->actual_tpl_dir. $this->tpl_name. '&nbsp;</b>&nbsp;couldn\'t be loaded!<br />' .
			 'Absolute Path: '.$this->actual_tpl_path);
	}
}


?>